home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 1.toast / pc / sample code / devices and hardware / adb / modifymouseaccl / modifymouseaccl.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-06-23  |  7.3 KB  |  265 lines

  1. /*
  2.     File:        ModifyMouseAccl.c
  3.     
  4.     Description:
  5.                 Snippet to demonstrate how one uses the CursorDevice Manager to change the 
  6.                 acceleration curve setting associated with the standard Apple mouse.
  7.     
  8.                 You can select an extremely sensitive or a tablet-like acceleration setting
  9.                 by setting the NOACCLCURVE define appropriately.
  10.  
  11.     Author:        RK
  12.  
  13.     Copyright:     Copyright: © 1984-1999 by Apple Computer, Inc.
  14.                 all rights reserved.
  15.     
  16.     Disclaimer:    You may incorporate this sample code into your applications without
  17.                 restriction, though the sample code has been provided "AS IS" and the
  18.                 responsibility for its operation is 100% yours.  However, what you are
  19.                 not permitted to do is to redistribute the source as "DSC Sample Code"
  20.                 after having made changes. If you're going to re-distribute the source,
  21.                 we require that you make it clear in the source that the code was
  22.                 descended from Apple Sample Code, but that you've made changes.
  23.     
  24.     Change History (most recent first):
  25.                 6/22/99 Updated for Metrowerks Codewarrior Pro 2.1
  26.  
  27. */
  28. #include <Types.h>
  29. #include <CursorDevices.h>
  30. #include "CrsrDev.h"
  31. #include <stdio.h>
  32. #include <LowMem.h>
  33.  
  34. #define NOACCLCURVE        0    // set to 0 for a really zippy mouse
  35.                             // set to 1 for a tablet style mouse - one with no acceleration
  36.  
  37. #define mapTrue            0xFFFF
  38. #define acclRsrcType    'accl'
  39. #define k200dpiMouseID    '@200'
  40.  
  41.     // the following points are from the ROM sources for the "@200"
  42.     // 200 dpi mouse accl setting for the upper curve
  43.     // leave the original values alone so that we can restore the 
  44.     // setting
  45.     // here are the original values    
  46. AcclPoint    gOrigAcclPt[9] =    {{0x0000713B, 0x00006000},
  47.                                  {0x00044EC5, 0x00108000},
  48.                                  {0x000C0000, 0x005F0000},
  49.                                  {0x0016EC4F, 0x008B0000},
  50.                                  {0x001D3B14, 0x00948000},
  51.                                  {0x00227627, 0x00960000},
  52.                                  {0x00246276, 0x00960000},
  53.                                  {0x00260000, 0x00960000},
  54.                                  {0x00280000, 0x00960000}};
  55.  
  56.  
  57.     // here are the replacement values for test
  58.     // modify these on a trial and error basis.
  59. #if NOACCLCURVE
  60. AcclPoint    gReplaceAcclPt[9] = {{0x00010000, 0x00010000},
  61.                                  {0x00010000, 0x00010000},
  62.                                  {0x00010000, 0x00010000},
  63.                                  {0x00010000, 0x00010000},
  64.                                  {0x00010000, 0x00010000},
  65.                                  {0x00010000, 0x00010000},
  66.                                  {0x00010000, 0x00010000},
  67.                                  {0x00010000, 0x00010000},
  68.                                  {0x00010000, 0x00010000}};
  69. #else
  70.  
  71. AcclPoint    gReplaceAcclPt[9] = {{0x00000400, 0x00006000},
  72.                                  {0x00000800, 0x00108000},
  73.                                  {0x00000C00, 0x00600000},
  74.                                  {0x00001000, 0x00A00000},
  75.                                  {0x00002000, 0x00C08000},
  76.                                  {0x00004000, 0x01000000},
  77.                                  {0x00008000, 0x02000000},
  78.                                  {0x0000C000, 0x04000000},
  79.                                  {0x00010000, 0x08000000}};
  80.  
  81. #endif
  82.  
  83. CursorDevicePtr    GetMouseCursorDeviceRec(UInt32 devID);
  84. AcclRsrcHandle    GetAcclRsrc(UInt32 devID);
  85. OSErr ModifySecond200dpiTable(AcclRsrcHandle acclRsrcHndl, 
  86.                         AcclPoint *acclPtr, short numPts);
  87.  
  88. main()
  89. {
  90.     CursorDevicePtr    cdPtr;
  91.     AcclRsrcHandle    acclRsrcHndl, saveHndl;
  92.     Fixed            accel = 0x00010000;
  93.     Size            hsize;
  94.     OSErr            err;
  95.     
  96.     cdPtr = GetMouseCursorDeviceRec((UInt32)k200dpiMouseID);
  97.     if (cdPtr == NULL)
  98.     {        // device not found
  99.         printf("\nDevice record for 200 dpi mouse not found");
  100.         return 1;
  101.     }
  102.     
  103.     acclRsrcHndl = GetAcclRsrc((UInt32)k200dpiMouseID);
  104.     if (acclRsrcHndl == NULL)
  105.     {        // device not found
  106.         printf("\naccl resource for 200 dpi mouse not found");
  107.         return 1;
  108.     }
  109.     
  110.         // save a copy of the handle
  111.     hsize = GetHandleSize((Handle)acclRsrcHndl);
  112.     if (hsize < 0)
  113.     {
  114.         printf("\nerror getting handle size");
  115.         return 1;
  116.     }
  117.  
  118.     saveHndl = (AcclRsrcHandle)NewHandle(hsize);
  119.     
  120.     if (saveHndl == nil)
  121.     {
  122.         printf("\nerror allocating the save handle");
  123.         return 1;
  124.     }
  125.  
  126.         // copy contents of the handle
  127.     BlockMove((Ptr)*acclRsrcHndl, (Ptr)*saveHndl, hsize);
  128.     
  129.         // check whether there are two tables for the 200 dpi accl resource
  130.     if ((*acclRsrcHndl)->numAcclTables <= 1)
  131.     {        // device not recognized
  132.         printf("\naccl resource for 200 dpi mouse not recognized");
  133.         return 1;
  134.     }
  135.  
  136.         // modify 200 dpi table using modified values
  137.     err = ModifySecond200dpiTable(acclRsrcHndl, (AcclPoint*)gReplaceAcclPt,
  138.                                 sizeof(gReplaceAcclPt)/sizeof(AcclPoint));
  139.     if (err != noErr)
  140.     {        // error adding modified values
  141.         printf("\nerror adding modified values - %d", err);
  142.         return 1;
  143.     }
  144.     
  145.         // the resource has been modified but we need to set the
  146.         // acceleration to have the modified accl values take effect
  147.     CursorDeviceSetAcceleration(cdPtr, accel);
  148.     
  149.     printf("\nthe modified accl field has been installed");
  150.     printf("\nclick the mouse when finished");
  151.     printf("\n");
  152.     printf("\n");
  153.     
  154.     while (!Button())
  155.             ;//Do Nothing
  156.  
  157.         // restore original accl values
  158.         
  159.         // copy contents of the handle
  160.     BlockMove((Ptr)*saveHndl, (Ptr)*acclRsrcHndl, hsize);
  161.  
  162.         // the resource has been modified but we need to set the
  163.         // acceleration to have the modified accl values take effect
  164.     CursorDeviceSetAcceleration(cdPtr, accel);
  165.     return 0;
  166. }
  167.  
  168. OSErr ModifySecond200dpiTable(AcclRsrcHandle acclRsrcHndl, AcclPoint *acclPtr,
  169.                                 short numPts)
  170. {
  171.     AcclTablePtr    acclTablePtr;
  172.     ResType            rType;
  173.     short            sizeTable1, sizeTable2;
  174.     //short            resSave, sysRefNum;
  175.     short            rID;
  176.     OSErr            err;
  177.     Str255            rName;
  178.  
  179.         // get the resource number for the accl resource
  180.     GetResInfo((Handle)acclRsrcHndl, &rID, &rType, rName);
  181.     if (err = ResError())
  182.         return err;
  183.         
  184.         // get the size of the first table
  185.     sizeTable1 = (*acclRsrcHndl)->acclTable[0].numAcclPoints;
  186.     sizeTable1--;    // subtract off the size of the first entry
  187.     
  188.         // set the acclTablePtr to point to the second table
  189.     acclTablePtr = (AcclTablePtr)((long)&(*acclRsrcHndl)->acclTable[1] + 
  190.                             sizeTable1 * sizeof(AcclPoint));
  191.                             
  192.         // get the number of accl points in the second table
  193.     sizeTable2 = acclTablePtr->numAcclPoints;
  194.         
  195.         // set sizeTable2 to the lesser of itself and numPts
  196.     sizeTable2 = sizeTable2 > numPts ? numPts : sizeTable2;
  197.     
  198.     BlockMove((Ptr)acclPtr, (Ptr)&acclTablePtr->acclPoints,
  199.                     sizeTable2 * sizeof(AcclPoint));
  200.     
  201.     return err;            
  202. /*
  203.     GetMouseCursorDeviceRec finds a CursorDevice record with a device ID matching
  204.     the devID input parameter
  205. */
  206. CursorDevicePtr    GetMouseCursorDeviceRec(UInt32 devID)
  207. {
  208.     CursorDevicePtr cdPtr;
  209.     Boolean            done = false;
  210.  
  211.     cdPtr = NULL;
  212.     while (done == false)
  213.     {
  214.         CursorDeviceNextDevice(&cdPtr);
  215.         if (cdPtr->devID == (OSType)devID)    // is this the record that we want
  216.             done = true;
  217.         else if (cdPtr->nextCursorDevice == NULL)    // is this the last record in the chain
  218.         {
  219.             done = true;
  220.             cdPtr = NULL;
  221.         }
  222. //        else    look at the next record
  223.     }
  224.     return cdPtr;
  225.  
  226. }
  227.  
  228. AcclRsrcHandle    GetAcclRsrc(UInt32 devID)
  229. {
  230.     AcclRsrcHandle    acclHndl;
  231.     short            numAcclRsrc;
  232.     short            i;
  233.     Boolean            done = false;
  234.     UInt8            romMapSave;
  235.     
  236.     romMapSave = LMGetROMMapInsert();    // get the state of the ROM Map Insert;
  237.     LMSetROMMapInsert(mapTrue);            // map the ROM into the resource chain
  238.  
  239.     numAcclRsrc = CountResources(acclRsrcType);
  240.     
  241.     i = 0;        // initialize our index
  242.     
  243.     while (done == false)
  244.     {
  245.         if (i >= numAcclRsrc)    // is the index past the number of 'accl' resources
  246.         {
  247.             done = true;        // let's bail
  248.             acclHndl = NULL;    // set result
  249.         }
  250.         else
  251.         {
  252.             i++;    // increment the counter since we start at 0
  253.             acclHndl = (AcclRsrcHandle)GetIndResource(acclRsrcType, i);
  254.             
  255.             if ((*acclHndl)->acclClass == (UInt32)devID)
  256.             {
  257.                 done = true;
  258.             }
  259.         }
  260.     }
  261.     
  262.     LMSetROMMapInsert(romMapSave);            // restore ROM Map insert state
  263.     return acclHndl;
  264. }